package com.yuanfeng.job.job;




import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.job.entity.JobDayOrderStaticsEntity;
import com.yuanfeng.job.entity.JobMonthOrderStaticsEntity;
import com.yuanfeng.job.service.JobDayOrderStaticsService;
import com.yuanfeng.job.service.JobMonthOrderStaticsService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class DayAndMonthStatisticsJob implements BaseJob {

    private static final Logger logger = LoggerFactory.getLogger(SalesPromotionsJob.class);

    @Autowired
    private JobDayOrderStaticsService jobDayOrderStaticsService;

    @Autowired
    private JobMonthOrderStaticsService jobMonthOrderStaticsService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        //今天0：0：0
        Date end = calendar.getTime();
        //昨天天0：0：0
        // 明天的就是1，昨天是负1
        calendar.add(Calendar.DATE, -1);
        Date start = calendar.getTime();
        
        //进行插入日统计操作
        Map<String,Double> dayMap = jobDayOrderStaticsService.countStatics(start,end);
        JobDayOrderStaticsEntity entity= ParseMapUtils.parseMap2Double(dayMap,JobDayOrderStaticsEntity.class);
        entity.setCreateTime(new Date());
        Boolean torf = jobDayOrderStaticsService.save(entity);
        logger.info("本次插入销售记录，开始时间:"+start+"结束时间"+end + "记录："
                +dayMap.toString() +"插入成功与否:" + torf );


        Calendar cal = Calendar.getInstance();
    	cal.setTime(new Date());
        if(cal.get(Calendar.DAY_OF_MONTH) != 0) {
            //当前月份
            Calendar monthCalendar = Calendar.getInstance();
            monthCalendar.setTime(new Date());
            monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
            monthCalendar.set(Calendar.HOUR_OF_DAY, 0);
            monthCalendar.set(Calendar.MINUTE, 0);
            monthCalendar.set(Calendar.SECOND, 0);
            Date endMonth = monthCalendar.getTime();
            monthCalendar.add(Calendar.MONTH,-1);
            monthCalendar.set(Calendar.HOUR_OF_DAY, 0);
            monthCalendar.set(Calendar.MINUTE, 0);
            monthCalendar.set(Calendar.SECOND, 0);
            monthCalendar.set(Calendar.DAY_OF_MONTH,1);
            Date startMonth = monthCalendar.getTime();
            //进行插入月统计操作
            Map<String,Double> monthMap = jobDayOrderStaticsService.countStatics(startMonth,endMonth);
            JobMonthOrderStaticsEntity monthOrderStaticsEntity= ParseMapUtils.parseMap2Double(dayMap,JobMonthOrderStaticsEntity.class);
            monthOrderStaticsEntity.setCreateTime(DateUtils.formateForSdfCn(new Date()));
            Boolean monthTorf = jobMonthOrderStaticsService.save(monthOrderStaticsEntity);
            logger.info("本次插入销售记录，开始时间:"+start+"结束时间"+end + "记录："
                    +monthMap.toString() +"插入成功与否:" + monthTorf);
         
            
            
        }


    }

}